import com.alibaba.moco.*
import com.alibaba.dto.*
import com.alibaba.moco.util.*
import script.moco.dto.*;
import redis.clients.jedis.*
import groovy.json.*


//模拟48小时自动扣款回调
def excute(data){

    Jedis redis = RedisPoolInstance.getRedisClient();
	def params = data.getParameterMap();
	def request = BaseTypeUtil.mapToBean(params,AutoCallbackRequest.class) as AutoCallbackRequest;//请求数据转换为对象
	
	def response = new AutoCallbackResponse(); 	

	def jsonStr = redis.get(request.getOrderId());	//从 Redis中获取 json 订单字符串	
	if(jsonStr==null){	//验证订单是否存在
		response.setSuccess("false");
		response.setErrorMsg("订单id不存在 !");
		response.setErrorCode(39001);
		response.setErrorLevel(1);
		
		return response;
	}
	
	def slurper = new JsonSlurper();			
	def detailMap = slurper.parseText(jsonStr);
	
	if (!detailMap.containsKey("order")){
		response.setSuccess("false");
		response.setErrorMsg("订单不存在 !");
		response.setErrorCode(39001);
		response.setErrorLevel(1);
		
		return response;
	}
	def orderStr  = redis.get(request.getOrderId())
    def orderJson = slurper.parseText(orderStr)
    orderJson.order.status = 700
    redis.set(request.getOrderId(), JsonOutput.toJson(orderJson).toString())

    def callbackDidiPayRequest = new CallbackDidiPayRequest()
    callbackDidiPayRequest.setClientId("1")
    callbackDidiPayRequest.setSign("1")
    callbackDidiPayRequest.setTimestamp(123456l)
    callbackDidiPayRequest.setOrderId(request.getOrderId())
    //从redis中获取info
    def info = redis.hget("DIDI.CALLBACK_INFO",request.getOrderId())
    callbackDidiPayRequest.setCallbackInfo(info)
    callbackDidiPayRequest.setClientSecret("secret")
    callbackDidiPayRequest.setNormalDistance(20)
    callbackDidiPayRequest.setPayTime(123456l)
    callbackDidiPayRequest.setStatus(700)
    callbackDidiPayRequest.setTotalPrice(new BigDecimal(orderJson.price.total_price))
    HttpClientUtil.postPay(callbackDidiPayRequest)

    response.setSuccess("true");
	response.setErrorMsg(null);
	response.setErrorCode(0);
	response.setErrorLevel(0);
	response.setContent(null);
    return response;
}